package com.example.myapplication.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.myapplication.R;
import com.example.myapplication.databinding.FragmentTitleBinding;
import com.example.myapplication.viewmodel.MyViewModel;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.SavedStateViewModelFactory;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.savedstate.SavedStateRegistryOwner;

public class TitleFragment extends Fragment implements SavedStateRegistryOwner {
    private MyViewModel myViewModel;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        myViewModel = ViewModelProviders.of(requireActivity(),new SavedStateViewModelFactory(requireActivity().getApplication(),
                requireActivity())).get(MyViewModel.class);


        FragmentTitleBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_title,container,false);
        binding.setData(myViewModel);
        binding.setLifecycleOwner(requireActivity());
        binding.btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //进入前设置当前得分为 0
                myViewModel.getCurrentScore().setValue(0);
                NavController navController = Navigation.findNavController(view);
                navController.navigate(R.id.action_titleFragment_to_questionFragment2);
            }
        });
        return binding.getRoot();
    }
}
